<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
            display: grid;
            grid-template-columns: 1fr 1fr;
        }

        .container {
            height: 50vh;
            outline: 1px solid black;
            position: relative;
        }

        .box {
            height: 100px;
            width: 100px;
            position: absolute;
            border-radius: 50%;
        }

        .b1 {
            background: red;
        }
        .b2 {
            background: green;
        }
        .b3 {
            background: orange;
        }
        .b4 {
            background: pink;
        }

    </style>
</head>
<body>

    <div class="container">
        <div class="box b1"></div>
    </div>
    <div class="container">
        <div class="box b2"></div>
    </div>
    <div class="container">
        <div class="box b3"></div>
    </div>
    <div class="container">
        <div class="box b4"></div>
    </div>


    <script>
        const b1 = document.querySelector('.box.b1');
        const b2 = document.querySelector('.box.b2');
        const b3 = document.querySelector('.box.b3');
        const b4 = document.querySelector('.box.b4');
        let isMouseDown = false;
        let selfX = 0;
        let selfY = 0;

        b1.addEventListener('mousedown', function (e) {
            isMouseDown = true;
            selfX = e.offsetX;
            selfY = e.offsetY;
        });
        document.addEventListener('mousemove', function (e) {
            // 如果鼠标没被按下
            if (!isMouseDown) {
                return;
            }
            
            const xMax = window.innerWidth * .5 - 100;
            const yMax = window.innerHeight * .5 - 100;
            const xMin = 0;
            const yMin = 0;
            const x = e.clientX - selfX;
            const y = e.clientY - selfY;
            
            let realX = x;
            if (x > xMax) {
                realX = xMax;
            } else if (x < xMin) {
                realX = xMin;
            }

            let realY = y;
            if (y > yMax) {
                realY = yMax;
            } else if (y < yMin) {
                realY = yMin;
            }
            
            b1.setAttribute('style', `
                top: ${ realY }px;
                left: ${ realX }px;
            `);
            b2.setAttribute('style', `
                top: ${ realY }px;
                left: ${ realX }px;
            `);
            b3.setAttribute('style', `
                top: ${ realY }px;
                left: ${ realX }px;
            `);
            b4.setAttribute('style', `
                top: ${ realY }px;
                left: ${ realX }px;
            `);
        });
        b1.addEventListener('mouseup', function () {
            isMouseDown = false;
        });

    </script>
</body>
</html>